| Conditions | 14 |
| Total Lines | 91 |
| Code Lines | 68 |
| Lines | 10 |
| Ratio | 10.99 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like EditFacturaCliente.js ➔ businessDocViewSave often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | |||
| 99 | async function businessDocViewSave() |
||
| 100 | { |
||
| 101 | $("#btn-document-save").prop("disabled", true); |
||
| 102 | |||
| 103 | var tipoPago = await cargarTipoPago(); |
||
| 104 | var datosPago = JSON.parse(tipoPago); |
||
| 105 | var ncfTipoPagoCliente = $("#formEditFacturaCliente select[name=ncftipopago]").val(); |
||
| 106 | var readOnlySelects = ($("#formSalesDocumentLine #doc_idestado").val() === '11')?true:false; |
||
| 107 | let selectOptionsPagos = ""; |
||
| 108 | View Code Duplication | $.each(datosPago.pagos, function(i, value) { |
|
| 109 | let defaultSelected = ((value.codigo === '17' && ncfTipoPagoCliente === '') || ncfTipoPagoCliente === value.codigo) ? 'selected' : ''; |
||
| 110 | let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : ''; |
||
| 111 | selectOptionsPagos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>'; |
||
| 112 | }); |
||
| 113 | |||
| 114 | var tipoMovimiento = await cargarTipoMovimiento(); |
||
| 115 | var datosMovimiento = JSON.parse(tipoMovimiento); |
||
| 116 | |||
| 117 | let selectOptionsMovimientos = ""; |
||
| 118 | View Code Duplication | $.each(datosMovimiento.movimientos, function(i, value) { |
|
| 119 | let defaultSelected = (value.codigo === '1') ? 'selected' : ''; |
||
| 120 | let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : ''; |
||
| 121 | selectOptionsMovimientos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>'; |
||
| 122 | }); |
||
| 123 | |||
| 124 | |||
| 125 | bootbox.dialog({ |
||
| 126 | title: "Complete la información faltante", |
||
| 127 | message: '<div class="form-content">\n' + |
||
| 128 | ' <form class="form" role="form">\n' + |
||
| 129 | ' <div class="form-group">\n' + |
||
| 130 | ' <label for="ncftipopago">Tipo de Pago</label>\n' + |
||
| 131 | ' <select class="custom-select" id="ncftipopago" name="ncftipopago"'+readOnlySelects+'>\n' + |
||
| 132 | selectOptionsPagos + |
||
| 133 | ' </select>\n' + |
||
| 134 | ' </div>\n' + |
||
| 135 | ' <div class="form-group">\n' + |
||
| 136 | ' <label for="ncftipomovimiento">Tipo de Movimiento</label>\n' + |
||
| 137 | ' <select class="custom-select" id="ncftipomovimiento" name="ncftipomovimiento"'+readOnlySelects+'>\n' + |
||
| 138 | selectOptionsMovimientos + |
||
| 139 | ' </select>\n' + |
||
| 140 | ' </div>\n' + |
||
| 141 | ' </form>\n' + |
||
| 142 | ' </div>', |
||
| 143 | buttons: [ |
||
| 144 | { |
||
| 145 | label: "Guardar", |
||
| 146 | className: "btn btn-primary", |
||
| 147 | callback: function() { |
||
| 148 | var data = {}; |
||
| 149 | $.each($("#" + businessDocViewFormName).serializeArray(), function (key, value) { |
||
| 150 | data[value.name] = value.value; |
||
| 151 | }); |
||
| 152 | data['ncftipopago'] = $('form #ncftipopago').val(); |
||
| 153 | data['ncftipomovimiento'] = $('form #ncftipomovimiento').val(); |
||
| 154 | data.action = "save-document"; |
||
| 155 | data.lines = getGridData(); |
||
| 156 | |||
| 157 | $.ajax({ |
||
| 158 | type: "POST", |
||
| 159 | url: businessDocViewUrl, |
||
| 160 | dataType: "text", |
||
| 161 | data: data, |
||
| 162 | success: function (results) { |
||
| 163 | if (results.substring(0, 3) === "OK:") { |
||
| 164 | $("#" + businessDocViewFormName + " :input[name=\"action\"]").val('save-ok'); |
||
| 165 | $("#" + businessDocViewFormName).attr('action', results.substring(3)).submit(); |
||
| 166 | } else { |
||
| 167 | alert(results); |
||
| 168 | $("#" + businessDocViewFormName + " :input[name=\"multireqtoken\"]").val(randomString(20)); |
||
| 169 | } |
||
| 170 | }, |
||
| 171 | error: function (msg) { |
||
| 172 | alert(msg.status + " " + msg.responseText); |
||
| 173 | } |
||
| 174 | }); |
||
| 175 | |||
| 176 | } |
||
| 177 | }, |
||
| 178 | { |
||
| 179 | label: "Cancelar", |
||
| 180 | className: "btn btn-danger", |
||
| 181 | callback: function() { |
||
| 182 | return true; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | ], |
||
| 186 | }); |
||
| 187 | |||
| 188 | $("#btn-document-save").prop("disabled", false); |
||
| 189 | } |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.